// pages/chooseMode/chooseMode.js
var httpClient = require('../../utils/httpClient.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    connectCode: '',
      connectStatus: false,
      shopId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

    asThreeScan: function () {
      var page = this
        wx.scanCode({
            success: (res) => {
              var urlParma = page.getRequest(res.result)
                page.setData({
                    connectCode: urlParma.connectCode,
                    shopId: urlParma.shopId,
                    connectStatus: true
                })
            }
        })
    },
    //获取url中"?"符后的字串
    getRequest: function (url) {
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(url.indexOf("?")+1, url.length);
            var strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    },
    scanSeries: function () {
        this.scan(this.scanSeries())
    },
    scan: function (callback) {
        var page = this
        wx.scanCode({
            success: (res) => {
                var req = {
                    connectCode: page.data.connectCode,
                    code: res.result,
                    shopId: page.data.shopId
                }
                httpClient.post('/scan/sendCodeToShop', req, function (result) {
                    if(callback){
                        callback
                    }
                });
            }
        })
    }
})